<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>文件上传</title>
    <style type="text/css">
        body {
            background-color: rgba(255, 255, 255, 0.1);
        }

        div {
            position: relative;
            width: 100%;
            height: 100%;
        }

        input[type="file"] {
            position: relative;
            cursor: pointer;
            opacity: 0;
        }

        img {
            display: none;
            width: 100px;
            height: 30px;
        }

        table {
            position: absolute;
            margin-left: 32%;
            margin-top: 24%;
            border: none;
            border-radius: 15px;
            text-align: center;
            background-color: rgba(255, 255, 255, 0.1);
            padding: 10px;
        }

        table td {
            padding-top: 15px;
        }

        .btn {
            padding: 3px 22px;
            border-radius: 25px;
            border: 0 solid;
            font-family: '楷体', serif;
            font-size: 18px;
            color: rgba(255, 255, 255, 0.6);
            background-color: rgba(255, 255, 255, 0.1);
        }

        .btn:active {
            position: relative;
            top: 1px;
        }

        .btn:focus {
            outline: 0;
        }

        .btn-upload {
            padding: 4px 15px;
            border: 0 solid;
            background-color: rgba(255, 255, 255, 0.1);
            color: rgba(255, 255, 255, 0.6);
        }

        .mask {
            position: absolute;
            width: 91%;
            height: 22%;
            text-align: left;
            font-family: '楷体', serif;
            font-size: 15px;
            color: rgba(255, 255, 255, 0.6);
        }

        .tip {
            position: absolute;
            margin-left: 43%;
            margin-top: 43%;
            width: auto;
        }
    </style>
</head>
<body>
<div>
    <div class="tip">
        <img src="./img/indexPage/load_success.png" alt="Picture" id="success">
        <img src="./img/indexPage/load_fail.png" alt="Picture" id="failure">
    </div>
    <table>
        <tr>
            <td>
                <div class="mask">
                    <input class="btn-upload" type="button" value="选择文件">
                    <span id="fileName">未选择文件</span>
                </div>
                <input type="file" name="fileUpload" id="fileUpload" onchange="getFileName();">
            </td>
        </tr>
        <tr>
            <td>
                <input class="btn" type="button" name="upload" id="upload" value="上传" onclick="UploadFile();">
            </td>
        </tr>
    </table>
</div>
<script>
    function getStrLen(str_, bool_, len_) {
        let len = 0, factLen = 0;
        for (let i = 0; i < str_.length; i++) {
            if (str_.charAt(i).match(/[^\x00-\xff]/ig) != null) {
                len += 2;
            } else {
                len += 1;
            }
            if (bool_) {
                factLen += 1;
                if (len > len_)
                    break;
            }
        }
        if (bool_)
            return factLen;
        return len;
    }

    function getFileName() {
        let name = document.getElementById("fileUpload").files[0].name;
        if (getStrLen(name, false, null) < 22)
            document.getElementById("fileName").innerText = name;
        else
            document.getElementById("fileName").innerText = name.substring(0, getStrLen(name, true, 9)) + "..."
                + name.substring(0, name.lastIndexOf(".")).slice(-2)
                + name.substring(name.lastIndexOf("."));
    }

    function setSpan() {
        document.getElementById("fileName").innerText = "未选择文件";
    }

    function showEle(id) {
        document.getElementById(id).style.display = "block";
    }

    function hiddenEle(id) {
        document.getElementById(id).style.display = "none";
    }

    function UploadFile() {
        let fileObj = document.getElementById("fileUpload").files[0];   // 获取文件对象
        if (fileObj != null) {
            let form = new FormData();
            form.append("file", fileObj);
            let xmlHttp = new XMLHttpRequest();
            xmlHttp.open("post", "upload" + "?types=" + Math.random(), false);
            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlHttp.onload = function () {
                if (xmlHttp.status === 200) {
                    if (xmlHttp.responseText === "true") {
                        showEle("success");
                        setTimeout("hiddenEle('success');", 3000);
                    } else {
                        showEle("failure");
                        setTimeout("hiddenEle('failure')", 3000);
                    }
                } else {
                    showEle("failure");
                    setTimeout("hiddenEle('failure')", 3000);
                }
            };
            xmlHttp.send(form);
        }
    }
</script>
</body>
</html>